package com.zmht.beike.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zmht.beike.core.BaseAction;
import com.zmht.beike.po.StudentTwo;
import com.zmht.beike.po.THabit;
import com.zmht.beike.util.TimeUtil;
import com.zmht.beike.web.form.HabitForm;
import com.zmht.specedu.vo.Teacher;

public class HabitAction extends BaseAction {
	/**
	 * 获得结果
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getHabit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
			String studentId = request.getParameter("studentId");
			THabit ht =null;
			if("edit".equals(request.getSession().getAttribute("action"))){
				ht = this.getHabitBiz().findByStudentId(studentId);
			}
			request.setAttribute("habit", ht);
			request.setAttribute("studentId", studentId);
		return new ActionForward("/jsp/pingce/ceping_habit.jsp");
	}
	/**
	 * 更新
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateHabit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
			String studentId = request.getParameter("studentId");
			HabitForm frm = (HabitForm)form;
			THabit th = new THabit();
			
			BeanUtils.copyProperties(th, frm);
			th.setCtime(TimeUtil.getYMDTime());
			if(new Integer(0).equals(th.getId()))
				this.getHabitBiz().saveHabit(th);
			else
				this.getHabitBiz().updateHabit(th);
			
			
			//更新学生记录
			StudentTwo student = (StudentTwo)this.getStudentBiz().getStudentDAO().findById(StudentTwo.class, new Integer(studentId));
			student.setSumHabit(th.getScore());
			student.setPingceDate(TimeUtil.getYMDTime());
			Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
			student.setWreteName(teacher.getTname());
			
			this.getStudentBiz().getStudentDAO().updateObj(student);
			//request.setAttribute("studentId", studentId);
		return new ActionForward("/beike/habit.do?method=getHabit&studentId="+studentId);
	}
	
}
